<?php

namespace catchAdmin\storehouse\model;

use catchAdmin\storehouse\model\search\InventoryOperationRecordSearch;
use catcher\base\CatchModel;
use think\facade\Db;
use think\facade\Log;
use catcher\CatchAuth;

/**
 *
 * @property int $id
 * @property string $warehousing_id
 * @property string $project_id
 * @property string $project_client
 * @property int $goods_number
 * @property string $customer_order_type
 * @property string $settlement
 * @property string $group_number
 * @property string $customer_business_number
 * @property string $relation_order
 * @property string $postposition_order
 * @property string $odd_numbers_one
 * @property string $odd_numbers_two
 * @property string $odd_numbers_three
 * @property string $entry_method
 * @property int $createtime
 * @property int $updatetime
 * @property int $deletetime
 * @property int $isstatus
 * @property int $sort
 * @property string $status
 * @property string $remarks
 * @property int $actual_number
 * @property int $admin_id
 */
class InventoryOperationRecord extends CatchModel
{
    use InventoryOperationRecordSearch;
    public $field = [
        'id',
        'order_type',
        'order_pid',
        'order_id',
        'warehousing_id',
        'warehousing_son_id',
        'goods_id',
        'goods_name',
        'tray_number',
        'box_number',
        'number',
        'stock_id',
        'tray_id',
        'box_id',
        'slice_id',
        'packing_id',
        'packing_specification',
        'project_id',
        'project_client',
        'created_at',
        'updated_at',
        'deleted_at',
        'tenant_id',
        'creator_id',
        'ext_json',
        'customer_business_number',
        'LOT1',
        'LOT2',
        'LOT3',
    ];

    public $name = 'db_inventory_operation_record';

    public function getList($tenant_id=0)
    {
        $where = [];
        if($tenant_id !== 0){
            $where[]=['r.tenant_id','=',$tenant_id];
        }

        return $this->catchSearch()
            ->alias('r')
            ->where($where)
            ->withoutField('tenant_id,updated_at,deleted_at')
            ->order('r.id', 'desc')
            ->paginate()
            ->each(function (&$item){
                $item['_num'] = $item['number'];
                if($item['order_type'] ==  \catchAdmin\storehouse\controller\InventoryOperationRecord::ORDER_TYPE_RK){
                    $item['order_type_text'] = '入库';
                }elseif($item['order_type'] ==  \catchAdmin\storehouse\controller\InventoryOperationRecord::ORDER_TYPE_CX){
                    $item['order_type_text'] = '拆箱';
                    $item['_num'] = $item['box_number'];
                }elseif($item['order_type'] ==  \catchAdmin\storehouse\controller\InventoryOperationRecord::ORDER_TYPE_CJ){
                    $item['order_type_text'] = '拆件';
                }elseif($item['order_type'] ==  \catchAdmin\storehouse\controller\InventoryOperationRecord::ORDER_TYPE_YK){
                    $item['order_type_text'] = '移库';
                }elseif($item['order_type'] ==  \catchAdmin\storehouse\controller\InventoryOperationRecord::ORDER_TYPE_CK){
                    $item['order_type_text'] = '出库';
                }

                $item['box_id'] = empty($item['box_id'])  ? '' : $item['box_id'];
                $item['slice_id'] = empty($item['slice_id'])  ? '' : $item['slice_id'];
                //$item['ext_json2arr'] = json_decode($item['ext_json'],true);

                $item['operator_name'] = Db::table('users')
                    ->where([['deleted_at','=',0],['id','=',$item['creator_id']]])
                    ->value('username');
            });
    }

    /**
     * 获取导出数据列表
     * 
     * @param int $page
     * @param int $limit
     * @return array
     */
    public function getExportList($page = 1, $limit = 1000)
    {

        $CatchAuth = new CatchAuth();
        $user = $CatchAuth->user();
        $type = $user['type'];
        $tenant_id = $user['tenant_id'];
        $user_id = $user->id;

        $where = [];
        if($tenant_id !== 0){
            $where[]=['r.tenant_id','=',$tenant_id];
        }

        return $this->catchSearch()
            ->alias('r')
            ->leftJoin('users u', 'r.creator_id = u.id')
            ->field([
                'r.id',
                'r.order_pid',
                'r.order_id', 
                'r.order_type',
                'r.goods_name',
                'r.packing_specification',
                'r.tray_number',
                'r.box_number',
                'r.number',
                'r.warehousing_id',
                'r.stock_id',
                'r.tray_id',
                'r.box_id',
                'r.slice_id',
                'r.customer_business_number',
                'r.LOT1',
                'r.LOT2',
                'r.LOT3',
                'u.username as operator_name',
                'r.created_at'
            ])
            ->where($where)
            ->page($page, $limit)
            ->select()
            ->toArray();
    }
}